<style lang="less" scoped>
  .monthly-trend{
    width: 100%;
    height: 100%;
    &_title{
      height: 40px;
      background: url('~@/images/contenttitle.png');
      background-repeat: no-repeat;
      background-position: bottom;
      text-align: center;
      color: #00F6FF;
      line-height: 40px;
      margin-bottom: 5px;
    }
    &_content{
      height: calc(100% - 45px);
    }
  }
</style>

<template>
  <div class="monthly-trend">
    <div class="monthly-trend_title">
      <span>{{ trendTitle }}</span>
    </div>
    <div id="monthlyTrend" class="monthly-trend_content" />
  </div>
</template>

<script>
import * as echarts from 'echarts'
import { getCABGNumMonth, getSurgeryNumMonth } from '@/api/comprehensive/monthlyTrend'
export default {
  props: {
    isStrock: {
      type: String,
      default: 'chestPain'
    }
  },
  data() {
    return {
      trendTitle: '再灌注措施月变化趋势',
      setInter: null
    }
  },
  watch: {
    'isStrock'() {
      this.init()
    }
  },
  mounted() {
    this.init()
    this.setInter = setInterval(() => {
      this.init()
    }, 5000)
  },
  destroyed() {
    clearInterval(this.setInter)
  },
  methods: {
    init() {
      const myChart = echarts.init(document.getElementById('monthlyTrend'))
      // myChart.clear()
      myChart.showLoading({
        text: '',
        fontSize: 18,
        left: 'center',
        color: 'transparent', // loading颜色，设置成透明或白色，不然会显示loading状态
        textColor: '#fff', // 文字颜色
        maskColor: 'rgba(255, 255, 255, 0)' // 背景色
      })
      window.addEventListener('resize', myChart.resize)
      if (this.isStrock == 'chestPain') {
        this.trendTitle = '再灌注措施月变化趋势'
        getCABGNumMonth({
          hospitalId: this.$store.getters.hospitalId
        }).then(res => {
          if (res.code == 0) {
            const timeLine = []
            const thisYearData = []
            const lastYearData = []
            if (res.data && res.data.length > 0) {
              res.data.forEach(el => {
                timeLine.push(el.month)
                thisYearData.push(el.patientNumThisYear)
                lastYearData.push(el.patientNumLastYear)
              })
              const option = {
                color: ['#59BCFB', '#01EEF6'],
                legend: {
                  type: 'plain',
                  top: 0,
                  icon: 'circle',
                  textStyle: {
                    color: '#fff'
                  },
                  formatter: function(name) {
                    return name
                  }
                },
                grid: {
                  top: '20%',
                  letf: '10%',
                  bottom: '20%'
                },
                xAxis: {
                  type: 'category',
                  axisLine: {
                    lineStyle: {
                      color: '#59BCFB33'
                    }
                  },
                  splitLine: {
                    show: false
                  },
                  axisTick: {
                    show: false
                  },
                  axisLabel: {
                    color: 'fff'
                  },
                  data: timeLine
                },
                yAxis: [{
                  name: '（例）',
                  type: 'value',
                  nameTextStyle: {
                    color: '#fff'
                  },
                  axisLabel: {
                    color: 'fff'
                  },
                  axisLine: {
                    lineStyle: {
                      color: '#59BCFB33'
                    }
                  },
                  splitLine: {
                    show: false
                  },
                  axisTick: {
                    show: false
                  }
                }],
                tooltip: {
                  trigger: 'axis',
                  textStyle: {
                    color: '#00F6FF'
                  },
                  axisPointer: {
                    type: 'cross',
                    crossStyle: {
                      color: '#999'
                    }
                  },
                  formatter: '{b}<br />{a0}: {c0}例<br />{a1}: {c1}例'
                },
                series: [
                  {
                    name: '再灌注措施例数',
                    type: 'line',
                    smooth: true,
                    label: {
                      show: true,
                      position: 'top'
                    },
                    data: thisYearData
                  },
                  {
                    name: '去年数据',
                    type: 'line',
                    smooth: true,
                    label: {
                      show: true,
                      position: 'top'
                    },
                    data: lastYearData
                  }
                ]
              }
              myChart.setOption(option)
            } else {
              myChart.showLoading({
                text: '暂无数据',
                fontSize: 18,
                left: 'center',
                color: 'transparent', // loading颜色，设置成透明或白色，不然会显示loading状态
                textColor: '#fff', // 文字颜色
                maskColor: 'rgba(255, 255, 255, 0)' // 背景色
              })
            }
          }
        })
      } else if (this.isStrock == 'stroke') {
        this.trendTitle = '手术例数变化趋势'
        getSurgeryNumMonth({
          hospitalId: this.$store.getters.hospitalId
        }).then(res => {
          if (res.code == 0) {
            const timeLine = []
            const thisYearData = []
            const lastYearData = []
            if (res.data && res.data.length > 0) {
              res.data.forEach(el => {
                timeLine.push(el.month)
                thisYearData.push(el.patientNumThisYear)
                lastYearData.push(el.patientNumLastYear)
              })
              const option = {
                color: ['#59BCFB', '#01EEF6'],
                legend: {
                  type: 'plain',
                  top: 0,
                  icon: 'circle',
                  textStyle: {
                    color: '#fff'
                  },
                  formatter: function(name) {
                    return name
                  }
                },
                grid: {
                  top: '20%',
                  letf: '10%',
                  bottom: '20%'
                },
                xAxis: {
                  type: 'category',
                  axisLine: {
                    lineStyle: {
                      color: '#59BCFB33'
                    }
                  },
                  splitLine: {
                    show: false
                  },
                  axisTick: {
                    show: false
                  },
                  axisLabel: {
                    color: 'fff'
                  },
                  data: timeLine
                },
                yAxis: [{
                  name: '（例）',
                  type: 'value',
                  nameTextStyle: {
                    color: '#fff'
                  },
                  axisLabel: {
                    color: 'fff'
                  },
                  axisLine: {
                    lineStyle: {
                      color: '#59BCFB33'
                    }
                  },
                  splitLine: {
                    show: false
                  },
                  axisTick: {
                    show: false
                  }
                }],
                tooltip: {
                  trigger: 'axis',
                  textStyle: {
                    color: '#00F6FF'
                  },
                  axisPointer: {
                    type: 'cross',
                    crossStyle: {
                      color: '#999'
                    }
                  },
                  formatter: '{b}<br />{a0}: {c0}例<br />{a1}: {c1}例'
                },
                series: [
                  {
                    name: 'AIS溶栓和血管内治疗',
                    type: 'line',
                    smooth: true,
                    label: {
                      show: true,
                      position: 'top'
                    },
                    data: thisYearData
                  },
                  {
                    name: '去年人次数',
                    type: 'line',
                    smooth: true,
                    label: {
                      show: true,
                      position: 'top'
                    },
                    data: lastYearData
                  }
                ]
              }
              myChart.setOption(option)
            } else {
              myChart.showLoading({
                text: '暂无数据',
                fontSize: 18,
                left: 'center',
                color: 'transparent', // loading颜色，设置成透明或白色，不然会显示loading状态
                textColor: '#fff', // 文字颜色
                maskColor: 'rgba(255, 255, 255, 0)' // 背景色
              })
            }
          }
        })
      }
    }
  }
}
</script>
